const jwt = require('jsonwebtoken');
const { sign } = require('./sign');
const Token = {
  encrypt: function (data, time) {
    console.log(data, time, 'jwt');
    //data加密数据，time过期时间
    return jwt.sign(data, sign, { expiresIn: time });
  },
  decrypt: function (token) {
    try {
      // token解密数据
      let tk = token.replace(`${sign} `, '');
      let data = jwt.verify(tk, sign);
      return {
        token: true,
        id: data.id,
      };
    } catch (e) {
      return {
        token: false,
        data: e,
      };
    }
  },
};
module.exports = Token;
